package com.example.exp_7;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StaffBpo {
    public static void insert(Context context,Staff staff) {
        SQLHelper sqlHelper = new SQLHelper(context,"staff.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();

        if (staff.getId().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入id",Toast.LENGTH_SHORT).show();
        } else if (staff.getName().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入姓名",Toast.LENGTH_SHORT).show();
        } else if (staff.getAge().equals("")) {
            Toast.makeText(context.getApplicationContext(),"请输入年龄",Toast.LENGTH_SHORT).show();
        } else if (staff.getHeight().equals("")) {
            Toast.makeText(context.getApplicationContext(),"请输入身高",Toast.LENGTH_SHORT).show();
        } else {
            String sql = "insert into staff(id,name,age,height) values(?,?,?,?)";
            db.execSQL(sql,new String[]{staff.getId(),staff.getName(),staff.getAge(),staff.getHeight()});
            db.close();
            Toast.makeText(context.getApplicationContext(),"添加成功",Toast.LENGTH_SHORT).show();
        }
    }
    public static void delete(Context context,String id) {
        SQLHelper sqlHelper = new SQLHelper(context,"staff.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        if (id.equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入id",Toast.LENGTH_SHORT).show();
        } else {
            String sql = "delete from staff where id = ?";
            try {
                db.execSQL(sql,new String[]{id});
            } catch (Exception e) {
                Toast.makeText(context.getApplicationContext(), "请输入合法的id",Toast.LENGTH_SHORT).show();
            } finally {
                db.close();
            }
        }
    }
    public static List<Map<String,String>> search(Context context, String id) {
        List<Map<String,String>> staffList = new ArrayList<>();
        SQLHelper sqlHelper = new SQLHelper(context,"staff.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();

        String sql = "select * from staff where id = ?";
        Cursor cursor = db.rawQuery(sql,null);
        while (cursor.moveToNext()) {
            Map<String,String> map = new HashMap<>();
            map.put("id",cursor.getString(0));
            map.put("name", cursor.getString(1));
            map.put("age", cursor.getString(2));
            map.put("height", cursor.getString(3));
            staffList.add(map);
        }
        return staffList;
    }
    public static void update(Context context,Staff staff) {
        SQLHelper sqlHelper = new SQLHelper(context,"staff.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();

        if (staff.getId().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入id",Toast.LENGTH_SHORT).show();
        } else if (staff.getName().equals("")) {
            Toast.makeText(context.getApplicationContext(), "请输入姓名",Toast.LENGTH_SHORT).show();
        } else if (staff.getAge().equals("")) {
            Toast.makeText(context.getApplicationContext(),"请输入年龄",Toast.LENGTH_SHORT).show();
        } else if (staff.getHeight().equals("")) {
            Toast.makeText(context.getApplicationContext(),"请输入身高",Toast.LENGTH_SHORT).show();
        } else {
            String sql = "update staff set name = ?,age = ?,height = ? where id = ?";
            db.execSQL(sql,new String[]{staff.getName(),staff.getAge(),staff.getHeight(),staff.getId()});
            db.close();
        }
    }
    public static List<Map<String,String>> display(Context context) {
        List<Map<String,String>> staffList = new ArrayList<>();
        SQLHelper sqlHelper = new SQLHelper(context,"staff.db",null,1);
        SQLiteDatabase db = sqlHelper.getReadableDatabase();
        String sql = "select * from staff";
        Cursor cursor = db.rawQuery(sql,null);
        while (cursor.moveToNext()) {
            Map<String,String> map = new HashMap<>();
            map.put("id",cursor.getString(0));
            map.put("name", cursor.getString(1));
            map.put("age", cursor.getString(2));
            map.put("height", cursor.getString(3));
            staffList.add(map);
        }
        return staffList;
    }
}
